﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;
using System.Data.Common;
using System.Data;

namespace Wjtour.BLL
{
    public class menu_bll : base_dll
    {
        public PaginationData<Wjtour.Domain.menu> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and title like '%" + Utility.GetSafeSql(q) + "%' ";
            //"select * from menu order by listorder desc,[mod] desc,[sort] desc";
            string sql = string.Format(@"select * from menu 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.menu> data = GetPaginationData<Wjtour.Domain.menu>(sql, page, pagesize);
            return data;
        }

        /// <summary>
        /// 获取某个角色附带访问权限的所有列表
        /// </summary>
        /// <param name="roleid">角色id</param>
        /// <returns></returns>
        public List<Wjtour.Domain.dto.menu_menuaccess> GetMenusWithAccessRightFlag(int roleid)
        {
            return GetAll<Wjtour.Domain.dto.menu_menuaccess>(string.Format(@"select m.*,isnull(ma.id,0) accessid from menu m
            left join menu_access ma on m.id=ma.menu_id and ma.role_id={0}
            order by m.listorder desc,m.[mod] desc,m.[sort] desc", roleid)).ToList();
        }

        public Wjtour.Domain.menu Load(int id)
        {
            string sql = "select * from menu where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.menu>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.menu entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE [menu] SET 
                [parent_id] = @parent_id,
                [node] = @node,
                [mod] = @mod,
                [act] = @act,
                [listorder] = @listorder,
                [sort] = @sort,
                [title] = @title,
                [hide] = @hide,
                [sys] = @sys
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);
            }
            else
            {
                sql = @"INSERT INTO [menu]( 
                [parent_id],[node],[mod],[act],[listorder],[sort],[title],[hide],[sys])VALUES(
                @parent_id,@node,@mod,@act,@listorder,@sort,@title,@hide,@sys)";
            }
            DbAccess.AddInParameter(cmd, "@parent_id", DbType.Int32, entity.parent_id);
            DbAccess.AddInParameter(cmd, "@node", DbType.String, entity.node);
            DbAccess.AddInParameter(cmd, "@mod", DbType.String, entity.mod);
            DbAccess.AddInParameter(cmd, "@act", DbType.String, entity.act);
            DbAccess.AddInParameter(cmd, "@listorder", DbType.Int32, entity.listorder);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@hide", DbType.Int16, entity.hide);
            DbAccess.AddInParameter(cmd, "@sys", DbType.Int16, entity.sys);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from menu where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
